<template>
  <view id="login">
    <view class="login_box card">
      <view class="login_title flex fw_600">
        <text class="fs_30 fw_700">欢迎登录WMS系统</text>
        <text class="fs_16">请先进行服务器地址配置</text>
      </view>
      <view class="card_body">
        <view class="my_input" v-if="isServe">
          <view class="flex">
            <image
              src="@/static/img/login_sever/server.png"
              class="mini_Icon"
            />
            <input
              type="text"
              v-model.trim="serverAddress"
              @confirm="setServer"
              placeholder="服务器地址"
              placeholder-style="font-size: 14px;color: rgb(219, 219, 219);"
            />
          </view>
          <image
            v-if="serverAddress"
            src="@/static/img/login_sever/right.png"
            class="mini_Icon icon"
          />
        </view>
      </view>
      <view class="card_footer">
        <zk-btn
          v-if="!isServe"
          backgroundColor="#05100c"
          class="fs_20"
          @tap="setSerev"
        >
          配置地址
        </zk-btn>
        <zk-btn class="enterSystem" @handleTap="enterSystem"> 进入页面 </zk-btn>
      </view>
    </view>
  </view>
  <zk-tips v-if="toastStore.show" :title="title"> </zk-tips>
</template>

<script lang="ts" setup>
import { useToastStore } from "@/stores/modules/toast";
import { useMemberStore } from "@/stores/modules/member";
import { setBaseURLIp } from "@/utils/api/http";
const userStore = useMemberStore();
const toastStore = useToastStore();
const height = uni.getStorageSync("contentHeight");
const title = ref(""); //错误提示
const isServe = ref(false); //配置服务器地址
const serverAddress = ref(userStore.baseIP); //服务器地址
const account = ref(""); //账号
const password = ref(""); //密码
//配置服务器地址
const setSerev = () => {
  isServe.value = true;
};
//设置服务器地址
const setServer = () => {
  setBaseURLIp(serverAddress.value);
  userStore.baseIP = serverAddress.value;
  console.log("setServer", serverAddress.value);
};
//进入系统
const enterSystem = async () => {
  if (serverAddress.value.trim() == "") {
    toastStore.show = true;
    title.value = "请输入服务器地址！";
    return;
  }
  setBaseURLIp(serverAddress.value);
  userStore.baseIP = serverAddress.value;
  uni.showLoading({
    title: "加载中...",
    mask: true,
  });

  const URL = "http://" + serverAddress.value + "/fieldmedical/ss/listGodown";
  uni.request({
    url: URL,
    data: { GodownStatus: "Normal" },
    method: "POST",
    header: {},
    timeout: 10000,
    success: (result: any) => {
      console.log("Success", result);
      setTimeout(() => {
        uni.hideLoading();
        uni.reLaunch({
          url: "/pages/index/index",
        });
      }, 1000);
    },
    fail: (err: any) => {
      console.error("Error", err);
      toastStore.show = true;
      toastStore.title = "服务器地址配置错误,或是网络错误；请检查后重试！";
      uni.hideLoading();
    },
  });
};
</script>

<style lang="scss" scoped>
#login {
  width: 100vw;
  height: 100vh;
  background: url("../../static/img/login_sever/bg.png");
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .login_box {
    border-radius: 8px;
    background: rgb(255, 255, 255);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 40px;
    .login_title {
      flex-direction: column;
      color: rgb(0, 7, 24);
      margin-bottom: 10px;
    }
    .card_body {
      width: 100%;
      .my_input {
        padding: 12px 0 12px 20px;
        position: relative;
        margin: 10px 0;
        input {
          padding: 0px 20px 0px 8px;
          width: 100%;
          height: 100%;
          font-size: 16px;
        }
        border: 1px solid rgb(219, 219, 219);
        border-radius: 10px;
        .icon {
          position: absolute;
          right: 10px;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
    .card_footer {
      margin-top: 20upx;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-evenly;
      .enterSystem {
        justify-content: center;
        width: 50%;
      }
    }
  }
}
</style>
